﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.DataServices.DataSources;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.Generic;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.UI.Layouts.ContentScreens;
using DiscoveryLogic.UI.Layouts.Models;
using DiscoveryLogic.UI.Layouts.Models;
using MortgageAccount=DiscoveryLogic.UI.Layouts.Models.MortgageAccount;
using Statement=DiscoveryLogic.UI.Layouts.Models.Statement;
using Transaction=DiscoveryLogic.UI.Layouts.Models.Transaction;

namespace DiscoveryLogic.UI.Layouts.LocalData
{
    public class DataFileGenerator
    {

        public static void CreateStatementData()
        {
            List<Statement> list = new List<Statement>();
            DateTime today = DateTime.Now;

            today = new DateTime(2007, 12, 28); 
            Random random = new Random();
            Random random2 = new Random(234123); 
            string [] Abbr = new string [] {"Jan", "Feb", "Mar", "Apr", "May", "Jun" , "Jul", "Aug", "Sept" , "Oct" , "Nov" , "Dec" } ;
            string[] Month = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; 

            double credit = 19823; 
            for (int i = 0; i < 12; i++)
            {
                Statement st = new Statement(); 
                DateTime dt = today.AddMonths ( -i ) ;
                st.Abbreviation = Abbr[dt.Month-1] + (dt.Year - 2000).ToString("D2");
                st.CreditAvailable = credit ;
                st.EndDate = dt;
                st.StartDate = dt.AddDays(-dt.Day);
                st.Title = Month[dt.Month-1] + " "  + dt.Year.ToString();

                List<Transaction> trans = new List<Transaction>(); 
                for (int j = 0; j < 28; j++)
                {
                    Transaction t = new Transaction();
                    double amount = random.NextDouble() * random2.NextDouble() * 352;
                    if (amount < 20.0 )
                        amount *= j; 

                    double format = Double.Parse ( amount.ToString("F2")); 
                    amount = format ; 
                    t.Description = "Transaction " + i + j; 
                    t.Date = dt.AddDays(-j);
                    if (j % 7 == 0)
                    {
                        if (amount < 400)
                            amount += 300;  
                        t.Deposit = amount ;                                                 

                        credit += t.Deposit;
                        
                    }
                    else
                    {
                        t.Withdrawal = amount ;
                        credit -= t.Withdrawal; 
                        
                    }
                    t.Balance = Double.Parse(credit.ToString("F2"));
                    credit = t.Balance; 

                    t.Amount = amount.ToString("F2");


                    if ( ( j% (dt.Day-i) == 0) && (j != 0) )
                    {
                        t.InfoTreatmentIndicator = true;
                        
                    } 
                    trans.Add(t); 
                }
                st.Transactions = trans.ToArray(); 
                list.Add ( st ) ; 
            }
            
            DataFileGenerator.SaveData<Statement[]>(list.ToArray(), "Statements.xml");

        }
        public static void CreateAccountData()
        {
            List<Account> listOfFakeAccounts = new List<Account>();
            // Creating Dummy Accounts                        
            listOfFakeAccounts.Add(new Account(1, "Premium Checking", "12345678", AccountType.Checking, 11030, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.CheckBook.xaml", DateTime.Today.Add(TimeSpan.FromDays(-1)), 1000));
            listOfFakeAccounts.Add(new Account(2, "Mortgage", "12345678", AccountType.Mortgage, 125000, 0, DateTime.Today.Add(TimeSpan.FromDays(27)), 1200, "DiscoveryLogic.UI.Layouts.Aspects.Mortgage.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(3, "Retirement Planner", "12345678", AccountType.RetirementPlanner, 2300, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.Plan.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(4, "529 Account", "12345678", AccountType.FiveTwoNine, 10000, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.529Account.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(5, "My 401 (k)", "12345678", AccountType.Investments, 10000, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.InvestmentPortfolio.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(6, "Woodgrove Gold Visa", "12345678", AccountType.CreditCard, 800, 80, DateTime.Today.Add(TimeSpan.FromDays(3)), 389, "DiscoveryLogic.UI.Layouts.Aspects.Visa.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(7, "Trade History", "12345678", AccountType.TradeHistory, 50000, 50, DateTime.Today.Add(TimeSpan.FromDays(30)), 0, "DiscoveryLogic.UI.Layouts.Aspects.TradeIcon.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(8, "Investment Portfolio", "12345678", AccountType.Investments, 3200, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.InvestmentPortfolio.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(9, "Savings", "12345678", AccountType.Savings, 6500, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.Savings.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(10, "529 Account", "12345678", AccountType.FiveTwoNine, 1120, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.529Account.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(11, "401 (k) Plan", "12345678", AccountType.Plan401k, 3000, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.401k.xaml", DateTime.MaxValue, 1000));
            listOfFakeAccounts.Add(new Account(12, "Savings", "12345678", AccountType.Savings, 2600, 0, DateTime.MaxValue, 0, "DiscoveryLogic.UI.Layouts.Aspects.Savings.xaml", DateTime.MaxValue, 1000));

            DataFileGenerator.SaveData<Account[]>(listOfFakeAccounts.ToArray(), "Accounts.xml");
        }

        public static void CreateBillPayData()
        {
            List<BillPayment> history = new List<BillPayment>();

            history.Add(new BillPayment(1220, new DateTime(2006, 10, 3), "12345678910"));
            history.Add(new BillPayment(788, new DateTime(2006, 11, 23), "12345678910"));
            history.Add(new BillPayment(677, new DateTime(2006, 12, 4), "12345678910"));
            history.Add(new BillPayment(399, new DateTime(2006, 12, 24), "12345678910"));
            history.Add(new BillPayment(100, new DateTime(2007, 1, 5), "12345678910"));
            history.Add(new BillPayment(449, new DateTime(2007, 2, 4), "12345678910"));
            history.Add(new BillPayment(555.96, new DateTime(2007, 7, 7), "12345678910"));
            history.Add(new BillPayment(676.23, new DateTime(2007, 9, 12), "12345678910"));
            history.Add(new BillPayment(576.23, new DateTime(2007, 9, 15), "12345678910"));
            history.Add(new BillPayment(1423.53, DateTime.Today.AddDays(15), "12345678910"));

            // Creating dummy data
            List<BillPayee> payeesList = new List<BillPayee>();

            payeesList.Add(new BillPayee("Testors", "33333333", false, history));
            payeesList.Add(new BillPayee("ABC Catering", "11111111", false, history));
            payeesList.Add(new BillPayee("Bell Telecom", "22222222", true, history));
            payeesList.Add(new BillPayee("Visa", "33333333", false, history));
            payeesList.Add(new BillPayee("TD Mortgage", "33333333", false, history));

            DataFileGenerator.SaveData<BillPayee[]>(payeesList.ToArray(), "BillPayee.xml");
        }

        public static void CreateMortgageData()
        {
            MortgageAccount m = new MortgageAccount(1, "abc1234", AccountType.Mortgage, 125000, 900, DateTime.Today.Add(TimeSpan.FromDays(27)), 1200, 4.9, 0, 600, 600, DateTime.Parse("01 Apr 2009"), null, 82092);
            m.PaymentSchedule = new AmortizationSchedule();
            m.PaymentSchedule.TotalPayments = 360;
            m.PaymentSchedule.StartDate = new DateTime(2001, 6, 1);
            m.ProjectedPaymentCompleteDate = new DateTime(2022, 6, 1);

            DataFileGenerator.SaveData<MortgageAccount>(m, "Mortgage.xml");

        }

        public static void CreateTradeNewsData()
        {
            /*
            List<TradeHistoryNewsItem> listOfNews = new List<TradeHistoryNewsItem>();

            listOfNews.Add(new TradeHistoryNewsItem(0, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-10)));
            listOfNews.Add(new TradeHistoryNewsItem(1, NewsTypes.Trivial, "News Headline", "Some media body text", DateTime.Today.AddDays(-20)));
            listOfNews.Add(new TradeHistoryNewsItem(2, NewsTypes.Industry, "News Headline", "Some media body text", DateTime.Today.AddDays(-26)));
            listOfNews.Add(new TradeHistoryNewsItem(3, NewsTypes.Worldwide, "News Headline", "Some media body text", DateTime.Today.AddDays(-47)));
            listOfNews.Add(new TradeHistoryNewsItem(4, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-63)));
            listOfNews.Add(new TradeHistoryNewsItem(5, NewsTypes.Trivial, "News Headline", "Some media body text", DateTime.Today.AddDays(-70)));
            listOfNews.Add(new TradeHistoryNewsItem(6, NewsTypes.Industry, "News Headline", "Some media body text", DateTime.Today.AddDays(-86)));
            listOfNews.Add(new TradeHistoryNewsItem(7, NewsTypes.Worldwide, "News Headline", "Some media body text", DateTime.Today.AddDays(-100)));
            listOfNews.Add(new TradeHistoryNewsItem(8, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-110)));

            DataFileGenerator.SaveData<TradeHistoryNewsItem[]>(listOfNews.ToArray(), "TradeHistoryNews.xml");
            */

            List<NewsItem> listOfNews = new List<NewsItem>();

            listOfNews.Add(new NewsItem(0, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-10)));
            listOfNews.Add(new NewsItem(1, NewsTypes.Trivial, "News Headline", "Some media body text", DateTime.Today.AddDays(-20)));
            listOfNews.Add(new NewsItem(2, NewsTypes.Industry, "News Headline", "Some media body text", DateTime.Today.AddDays(-26)));
            listOfNews.Add(new NewsItem(3, NewsTypes.Worldwide, "News Headline", "Some media body text", DateTime.Today.AddDays(-47)));
            listOfNews.Add(new NewsItem(4, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-63)));
            listOfNews.Add(new NewsItem(5, NewsTypes.Trivial, "News Headline", "Some media body text", DateTime.Today.AddDays(-70)));
            listOfNews.Add(new NewsItem(6, NewsTypes.Industry, "News Headline", "Some media body text", DateTime.Today.AddDays(-86)));
            listOfNews.Add(new NewsItem(7, NewsTypes.Worldwide, "News Headline", "Some media body text", DateTime.Today.AddDays(-100)));
            listOfNews.Add(new NewsItem(8, NewsTypes.Company, "News Headline", "Some media body text", DateTime.Today.AddDays(-110)));

            DataFileGenerator.SaveData<NewsItem[]>(listOfNews.ToArray(), "TradeHistoryNews.xml");
        }

        public static void CreateTradeData()
        {
            List<Trade> source = new List<Trade>();

            int id = 0;

            for (int i = 0; i < 4; i++)
            {
                source.Add(new Trade(id++, "Microsoft", "MSFT", 100, 100, 10000, DateTime.Today.AddDays(-2 * (i + 1)).ToString("MM/dd/yyyy"), true));
                source.Add(new Trade(id++, "General Electric", "GE", 100, 100, 10000, DateTime.Today.AddDays(-11 * (i + 1)).ToString("MM/dd/yyyy"), true));
                source.Add(new Trade(id++, "Wal-Mart", "WMT", 100, 100, 10000, DateTime.Today.AddDays(-23 * (i + 1)).ToString("MM/dd/yyyy"), true));
                source.Add(new Trade(id++, "Lehman Bros.", "LEH", 100, 100, 10000, DateTime.Today.AddDays(-27 * (i + 1)).ToString("MM/dd/yyyy"), false));
                source.Add(new Trade(id++, "Microsoft", "MSFT", 300, 100, 30000, DateTime.Today.AddDays(-31 * (i + 1)).ToString("MM/dd/yyyy"), false));

            }

            DataFileGenerator.SaveData<Trade[]>(source.ToArray(), "TradeHistoryTrades.xml");

        }

        public static void CreateFundsData()
        {

            List<Fund> listOfLocalFunds = new List<Fund>();

            for (int i = 0; i < 10; i++)
            {
                Fund fundTab;

                int id = i;

                // Determine enrollment type
                if (i < 3)
                {
                    fundTab = new Fund(id, FundTypes.ForeignEquity, "Fund " + Convert.ToChar(66 + i).ToString(), 0, 0);
                }
                else if (i < 6)
                {
                    fundTab = new Fund(id, FundTypes.DomesticBonds, "Fund " + Convert.ToChar(66 + i).ToString(), 0, 0);
                }
                else if (i < 9)
                {
                    fundTab = new Fund(id, FundTypes.Other, "Fund " + Convert.ToChar(66 + i).ToString(), 0, 0);
                }
                else
                {
                    fundTab = new Fund(id, FundTypes.ShortTerm, "Fund " + Convert.ToChar(66 + i).ToString(), 0, 0);
                }

                listOfLocalFunds.Add(fundTab);
            }

            DataFileGenerator.SaveData<Fund[]>(listOfLocalFunds.ToArray(), "Funds.xml");
        }

        private static void SaveData<T>(T data, string filename)
        {
            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();


            IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(filename, FileMode.Create, isoStore);

            serializer.WriteObject(isoStream, data);
 
            isoStream.Close();

        }
    }
}
